﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="NewProjectWindow.xaml.cs" company="Mar3ek">
//   http://mar3ek.wordpress.com
// </copyright>
// <summary>
//   Interaction logic for NewProjectWindow.xaml
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Mar3ek.ColorHatTimeTracker
{
    using System.Linq;
    using System.Windows;

    /// <summary>
    /// Interaction logic for NewProjectWindow
    /// </summary>
    public partial class NewProjectWindow
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="NewProjectWindow"/> class.
        /// </summary>
        public NewProjectWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// The OkButton.Click event handler.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The event arguments.
        /// </param>
        private async void OkButtonClick(object sender, RoutedEventArgs e)
        {
            var vm = (NewProjectViewModel)this.DataContext;
            if (!vm.Api.Customers.Values.Contains(vm.SelectedCustomer))
            {
                var result = MessageBox.Show(string.Format("The customer '{0}' was not found in your Colorhat account. Do you wish to create it now?", vm.SelectedCustomer), "Colorhat Time Tracker", MessageBoxButton.YesNo, MessageBoxImage.Question);

                if (result == MessageBoxResult.Yes)
                {
                    var customerCreated = await vm.Api.CreateCustomer(vm.SelectedCustomer, null);

                    if (!customerCreated)
                    {
                        MessageBox.Show("Customer creation failed.", "Colorhat Time Tracker", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }
                }
                else
                {
                    return;
                }
            }

            this.DialogResult = true;
            this.Close();
        }

        /// <summary>
        /// The CancelButton.Click event handler.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The event arguments.
        /// </param>
        private void CancelButtonClick(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }
    }
}
